import { defineStore } from 'pinia'
import menuRouters from '@/router/menu-router'; // 展示在菜单中的路由
import sideBarRouter from '@/router/sidebar-router';
import { menuPermissonsFilter } from '@/utils/permission'

const useMenuStore = defineStore('menu', {
    state: () => ({
        sidebarCollapse: true,
        routers: [],
        sidebarMenus:[],
    }),
    actions: {
        setRouters(routers) {
            this.routers = menuRouters.concat(routers)
        },
        setSidebarMenus(menus) {
            this.sidebarMenus = menus
        },
        toggleSideBar() {
            this.sidebarCollapse = !this.sidebarCollapse
        },
        // 生成路由
        generateRoutes(routers) {
            return new Promise(async resolve => {
                const accessedRoutes=[]
                let menuPermissions = menuPermissonsFilter(sideBarRouter,[])
                accessedRoutes.push({path: '*', redirect: '/404', hidden: true})
                this.setRouters(accessedRoutes)
                this.setSidebarMenus(menuPermissions)
                resolve(accessedRoutes)
            })
        }
    },
    getters: {
        getSidebarMenus: (state) => {
            return state.sidebarMenus
        },
        getSideBarCollapse: (state) => {
            return state.sidebarCollapse
        },
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'menu',
                storage: localStorage,
                path:[] //可以选择保存的字段  其余的不保存
            }
        ]
    }
})

export default useMenuStore
